package cn.edu.kzjsxy.graduationproject.common;

import lombok.Data;

import java.io.Serializable;

/**
 * @Author：chén jiā jùn
 * @Create：2024年03月27日17:35
 * 统一返回给前端的格式
 */
@Data
public class Result<T> implements Serializable {

	private Integer code;
	private String msg;
	private T data;

	public static <T> Result<T> success() {  //成功
		Result<T> result = new Result<>();
		result.setCode(ResultCode.SUCCESS.getCode());
		result.setMsg(ResultCode.SUCCESS.getMessage());
		return result;
	}

	public static <T> Result<T> success(T object) { //成功
		Result<T> result = new Result<>();
		result.setData(object);
		result.setCode(ResultCode.SUCCESS.getCode());
		result.setMsg(ResultCode.SUCCESS.getMessage());
		return result;
	}

	public static <T> Result<T> error(ResultCode resultCode) {// 失败
		Result<T> result = new Result<>();
		result.setMsg(resultCode.getMessage());
		result.setCode(resultCode.getCode());
		return result;
	}

	public static <T> Result<T> error(String msg) {// 失败
		Result<T> result = new Result<>();
		result.setMsg(msg);
		result.setCode(ResultCode.FAIL.getCode());
		return result;
	}

	public static <T> Result<T> judgment(boolean outcome) {// 判断是成功还是失败！
		Result<T> result = new Result<>();
		if(outcome){
			result.setCode(ResultCode.SUCCESS.getCode());
			result.setMsg(ResultCode.SUCCESS.getMessage());
		}else{
			result.setMsg(ResultCode.FAIL.getMessage());
			result.setCode(ResultCode.FAIL.getCode());
		}
		return result;
	}
}
